<?php
namespace Application\Api\Controller;

class UserBank extends Base
{

    private function getService()
    {
        return $this->get(SERVICE_USERBANK);
    }

    /*
        /Api/UserBank/add
    */
    public function addAction()
    {
        $data = $this->getRequestData();
        if(notSetOrEmpty($data, 'device') || notSetOrEmpty($data, 'token') || notSetOrEmpty($data, 'name') || notSetOrEmpty($data, 'idCard') || notSetOrEmpty($data, 'bankCard') || notSetOrEmpty($data, 'phone')){
            $this->sendFail('PARAMETER_ERROR');
            return false;
        }
        $device = $data['device'];
        $name = $data['name'];
        $idCard = $data['idCard'];
        $bankCard = $data['bankCard'];
        $phone = $data['phone'];
        if(!isIdCard($idCard)){
            $this->sendFail('ID_CARD_ERROR');
            return false;
        }
        if(!isBankCard($bankCard)){
            $this->sendFail('BANK_CARD_ERROR');
            return false;
        }
        if(!isPhone($phone)){
            $this->sendFail('PHONE_ERROR');
            return false;
        }
        /*判断token*/
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        /*银行卡号是否正确*/
        $serviceFinance = $this->get(SERVICE_FINANCE);
        $response = $serviceFinance->bankQuery($bankCard);
        if(notSetOrEmpty($response, 'validated') || $response['validated'] != 1){
            $this->sendFail('BANK_CARD_ERROR');
            return false;
        }
        $bankCode = $response['bank'];
        /*是否是支持的银行*/
        $serviceBank = $this->get(SERVICE_BANK);
        if(!$serviceBank->isSupport($bankCode)){
            $this->sendFail('FAIL', '该银行卡还未支持');
            return false;
        }
        /*每家银行只能添加1张卡*/
        $serviceUserBank = $this->getService();
        if($serviceUserBank->hasAddByBankCode($userId, $bankCode)){
            $this->sendFail('FAIL', '每家银行只能添加1张卡');
            return false;
        }
        /*验证四元素(优先缓存判断)*/
        $serviceBank4Element = $this->get(SERVICE_BANK4ELEMENT);
        $unique = md5($name . $idCard . $bankCard . $phone);
        $bank4Element = $serviceBank4Element->getOneByUnique($unique);
        if(!$bank4Element){
            $serviceJuHe = $this->get(SERVICE_JUHE);
            $raw = $serviceJuHe->bank4element($bankCard, $name, $idCard, $phone);
            $rawObject = json_decode($raw);
            $result = $rawObject->result->res;
            $id = $serviceBank4Element->addData($unique, $name, $idCard, $bankCard, $phone, $result, $raw);
            $bank4Element = $serviceBank4Element->one($id);
        }
        if($bank4Element->dry_result != 1){
            $this->sendFail('FAIL', '四元素信息不匹配');
            return false;
        }
        else{
            $serviceUserBank->addData($userId, $name, $idCard, $bankCard, $bankCode, $phone);
            $this->sendSuccess();
        }
        $this->sendFail();
    }

    /*
        /Api/UserBank/getList
    */
    public function getListAction()
    {
        $data = $this->getRequestData();
        if(notSetOrEmpty($data, 'device')){
            $this->sendFail('PARAMETER_ERROR', '', __LINE__);
            return false;
        }
        $device = $data['device'];
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $page = (int)tryGet($data, 'page', 1);
        $limit = (int)tryGet($data, 'limit', 20);
        $service = $this->getService();
        $temp = $service->getList($userId, $page, $limit);
        $list = $temp['list'];
        $list = $service->decrypt($list);
        $list2 = $this->get(SERVICE_BANK)->getAll();
        $map = array2mapObject($list2, 'dry_code');
        foreach($list as $k => $v){
            $v->bank_name = '';
            if(isset($map[$v->dry_bank_code])){
                $v->bank_name = $map[$v->dry_bank_code]->dry_name;
            }
            $v->sensitive_id_card = getSensitiveIdCard($v->dry_id_card);
            $v->sensitive_bank_card = getSensitiveBankCard($v->dry_bank_card);
            $list[$k] = $v;
        }
        $send = [
            'list' => $list
        ];
        $send = addPageInformation($send, $page, $limit, $temp['count']);
        $this->sendSuccess($send);
    }

    /*
        /Api/UserBank/delete
    */
    public function deleteAction()
    {
        $data = $this->getRequestData();
        if(notSetOrEmpty($data, 'device') || notSetOrEmpty($data, 'token') || notSetOrEmpty($data, 'id')){
            $this->sendFail('PARAMETER_ERROR', '', __LINE__);
            return false;
        }
        $device = $data['device'];
        $id = (int)$data['id'];
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $service = $this->getService();
        $userBank = $service->one($id);
        if($userBank && $userBank->dry_user == $userId){
            $service->delete($id);
        }
        $this->sendSuccess();
    }

}